#' 获得opengwas及其他数据库所有ID的信息
#'
#' @param database 默认为"opengwas"
#'
#' @return 所有ID的信息
#' @export
#'
#' @examples
#'
#'
#'
#' # 默认opengwas的id
#' ao<-U1_get_ao()
#'
#' # 单个的finngen_R10
#' database= c("finngen_R10" )
#' ao<-U1_get_ao(database)
#'
#' # 所有的
#' database= c("opengwas","finngen_R10","finngen_R9","finngen_R8","finngen_R7","nealelab2")
#' ao<-U1_get_ao(database)
#'
#' # 或者
#' ao<-U1_get_ao(database="all")
#'
#' # 除了opengwas的id以外，其余都是需要下载本地文件的ID
#'
#'
#'
#'
U1_get_ao<-function(database="opengwas"){

  if( identical(database,"all" ) ){
    database= c("opengwas","finngen_R10","finngen_R9","finngen_R8","finngen_R7","nealelab2")
  }

  dat <- database %>%
    split(list(. ) )

  ao_tab <- dat  %>%
    furrr::future_map_dfr( get_data ,.options = furrr::furrr_options(seed = TRUE),.progress = TRUE
    )

  require(dplyr)
  ao_tab<-ao_tab %>%
    relocate(any_of(c("class", "id", "trait", "ncase", "ncontrol","sample_size","sex","population","pmid","url","File" ) )
    )
  return(ao_tab)
}
get_data <- function( data ){
  class(data)<-c(data,class(data) )
  return( get_data_inter(data) )
}
get_data_inter<-function(data){
  UseMethod( "get_data_inters" )
}
get_data_inters.opengwas<-function(data){

  if (file.exists(paste0(file.path(tools::R_user_dir("Oneclick",
                                                     which = "data"), "path.Rdata")))) {
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))

  } else {
    stop("请使用U1_set_file_path功能设置您的附加文件路径!")
  }

  filetime<-file.mtime(file.path(path, "gwasdata/ao.csv"))
  time_diff <- difftime(Sys.time(), filetime, units = "days")

  update_ao<-function(){
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))
    ao<- TwoSampleMR::available_outcomes()
    write.csv( ao,file.path(path, "gwasdata/ao.csv") )
  }

  if (as.numeric(time_diff) > 7) {

    message("超过一周自动更新gwasdata/ao.csv文件")

    tryCatch({
      update_ao()
    }, error = function(e) message("自动更新失败") )

  }

  ao <- read.csv( file.path(path, "gwasdata/ao.csv") )[,-1]
  message("读取数据成功")
  ao$class <- "opengwas"
  return(ao)
}

get_data_inters.finngen_R10<-function(data){

  if (file.exists(paste0(file.path(tools::R_user_dir("Oneclick",
                                                     which = "data"), "path.Rdata")))) {
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))

  } else {
    stop("请使用U1_set_file_path功能设置您的附加文件路径!")
  }


  finngen_R10 <- data.table::fread( file.path(path, "gwasdata/芬兰数据库R10.tsv") )

  require(dplyr)
  finngen_R10_format<-finngen_R10 %>%
    rowwise() %>%
    mutate(id= strsplit(path_https,"https://storage.googleapis.com/finngen-public-data-r10/summary_stats/")[[1]][2] )%>%
    mutate(File=id) %>%
    mutate(id= strsplit(id,".gz")[[1]][1] ) %>%
    rename(trait = phenotype,
           ncase = num_cases,
           ncontrol = num_controls,
           url = path_https,
           subcategory =  category ,
           note =  phenocode) %>%
    mutate(sample_size = ncase + ncontrol,
           sex='Males and Females',
           population="European")
  finngen_R10_format$class <- "finngen_R10"

  finngen_R10_format$ncontrol[ which(finngen_R10_format$ncontrol == 0 )] <-NA
  finngen_R10_format$ncase[ which(finngen_R10_format$ncontrol == 0 )] <-NA
  return(finngen_R10_format)
}

get_data_inters.finngen_R9<-function(data){

  if (file.exists(paste0(file.path(tools::R_user_dir("Oneclick",
                                                     which = "data"), "path.Rdata")))) {
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))

  } else {
    stop("请使用U1_set_file_path功能设置您的附加文件路径!")
  }


  finngen_R9 <- data.table::fread( file.path(path, "gwasdata/芬兰数据库R9.tsv") )

  require(dplyr)
  finngen_R9_format<-finngen_R9 %>%
    rowwise() %>%
    mutate(id= strsplit(path_https,"https://storage.googleapis.com/finngen-public-data-r9/summary_stats/")[[1]][2] )%>%
    mutate(File=id) %>%
    mutate(id= strsplit(id,".gz")[[1]][1] ) %>%
    rename(trait = name,
           ncase = num_cases,
           ncontrol = num_controls,
           url = path_https,
           subcategory =  category ,
           note =  phenocode) %>%
    mutate(sample_size = ncase + ncontrol,
           sex='Males and Females',
           population="European")
  finngen_R9_format$class <- "finngen_R9"

  finngen_R9_format$ncontrol[ which(finngen_R9_format$ncontrol == 0 )] <-NA
  finngen_R9_format$ncase[ which(finngen_R9_format$ncontrol == 0 )] <-NA
  return(finngen_R9_format)
}

get_data_inters.finngen_R8<-function(data){

  if (file.exists(paste0(file.path(tools::R_user_dir("Oneclick",
                                                     which = "data"), "path.Rdata")))) {
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))

  } else {
    stop("请使用U1_set_file_path功能设置您的附加文件路径!")
  }


  finngen_R8 <- data.table::fread( file.path(path, "gwasdata/芬兰数据库R8.tsv") )

  require(dplyr)
  finngen_R8_format<-finngen_R8 %>%
    rowwise() %>%
    mutate(id= strsplit(path_https,"https://storage.googleapis.com/finngen-public-data-r8/summary_stats/")[[1]][2] )%>%
    mutate(File=id) %>%
    mutate(id= strsplit(id,".gz")[[1]][1] ) %>%
    rename(trait = name,
           ncase = num_cases,
           ncontrol = num_controls,
           url = path_https,
           subcategory =  category ,
           note =  phenocode) %>%
    mutate(sample_size = ncase + ncontrol,
           sex='Males and Females',
           population="European")
  finngen_R8_format$class <- "finngen_R8"

  finngen_R8_format$ncontrol[ which(finngen_R8_format$ncontrol == 0 )] <-NA
  finngen_R8_format$ncase[ which(finngen_R8_format$ncontrol == 0 )] <-NA
  return(finngen_R8_format)
}

get_data_inters.finngen_R7<-function(data){

  if (file.exists(paste0(file.path(tools::R_user_dir("Oneclick",
                                                     which = "data"), "path.Rdata")))) {
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))

  } else {
    stop("请使用U1_set_file_path功能设置您的附加文件路径!")
  }


  finngen_R7 <- data.table::fread( file.path(path, "gwasdata/芬兰数据库R7.tsv") )

  require(dplyr)
  finngen_R7_format<-finngen_R7 %>%
    rowwise() %>%
    mutate(id= strsplit(path_https,"https://storage.googleapis.com/finngen-public-data-r7/summary_stats/")[[1]][2] )%>%
    mutate(File=id) %>%
    mutate(id= strsplit(id,".gz")[[1]][1] ) %>%
    rename(trait = name,
           ncase = n_cases,
           ncontrol = n_controls,
           url = path_https,
           subcategory =  category ,
           note =  phenocode) %>%
    mutate(sample_size = ncase + ncontrol,
           sex='Males and Females',
           population="European")
  finngen_R7_format$class <- "finngen_R7"

  finngen_R7_format$ncontrol[ which(finngen_R7_format$ncontrol == 0 )] <-NA
  finngen_R7_format$ncase[ which(finngen_R7_format$ncontrol == 0 )] <-NA
  return(finngen_R7_format)
}

get_data_inters.nealelab2<-function(data){

  if (file.exists(paste0(file.path(tools::R_user_dir("Oneclick",
                                                     which = "data"), "path.Rdata")))) {
    load(paste0(file.path(tools::R_user_dir("Oneclick",
                                            which = "data"), "path.Rdata")))

  } else {
    stop("请使用U1_set_file_path功能设置您的附加文件路径!")
  }


  nealelab2 <- rio::import( file.path(path, "gwasdata/UKBB GWAS Imputed v3 - File Manifest Release 20180731.xlsx"), sheet = "Manifest 201807" )

  require(dplyr)

  suppressMessages( biomarkers1 <-   readr::read_tsv( file.path(path, "gwasdata/nearlab extra file/biomarkers.both_sexes.tsv.bgz") ) )
  biomarkers1$Sex <- 'both_sexes'
  suppressMessages( biomarkers2 <-   readr::read_tsv( file.path(path, "gwasdata/nearlab extra file/biomarkers.female.tsv.bgz") ))
  biomarkers2$Sex <- 'female'
  suppressMessages( biomarkers3 <-   readr::read_tsv( file.path(path, "gwasdata/nearlab extra file/biomarkers.male.tsv.bgz") ))
  biomarkers3$Sex <- 'male'
  suppressMessages( biomarkers4 <-   readr::read_tsv( file.path(path, "gwasdata/nearlab extra file/phenotypes.both_sexes.v2.tsv.bgz") ))
  biomarkers4$Sex <- 'both_sexes'
  suppressMessages( biomarkers5 <-   readr::read_tsv( file.path(path, "gwasdata/nearlab extra file/phenotypes.female.v2.tsv.bgz") ))
  biomarkers5$Sex <- 'female'
  suppressMessages( biomarkers6 <-   readr::read_tsv( file.path(path, "gwasdata/nearlab extra file/phenotypes.male.v2.tsv.bgz") ))
  biomarkers6$Sex <- 'male'




  biomarkers <-plyr::rbind.fill(biomarkers1, biomarkers2, biomarkers3,
                                biomarkers4, biomarkers5, biomarkers6
  )

  biomarkers <- biomarkers %>%
    rename(`Phenotype Code`= phenotype,
           `Phenotype Description`=description)


  nealelab2_new <- dplyr::left_join(nealelab2[-c(1:22),], biomarkers, by = c('Phenotype Code','Phenotype Description',"Sex"))



  nealelab2_new_format<-nealelab2_new %>%
    rowwise() %>%
    mutate(id= strsplit(File,".tsv.bgz")[[1]][1] ) %>%
    rename(trait = `Phenotype Description`,
           ncase = n_cases,
           ncontrol = n_controls,
           url = `AWS File` ,
           note =  `Phenotype Code`,
           category=variable_type,
           sex = Sex)  %>%
    mutate(sample_size = n_non_missing,
           class = "nealelab2",
           population="European")

  # unique(nealelab2_new_format$sex)
  nealelab2_new_format$sex=recode(nealelab2_new_format$sex,
                                  both_sexes = 'Males and Females',
                                  female = "Females",
                                  male = "Males")

  return(nealelab2_new_format)
}

